package grape.auth.rest.auth2;

import com.google.common.collect.Maps;
import grape.common.service.common.dataconstraint.RawDataConstraint;
import grape.common.service.loginuser.LoginUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * token增强信息
 */
@Component
public class TokenEnhancer implements org.springframework.security.oauth2.provider.token.TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Map<String, Object> additionalInfo = Maps.newHashMap();
        //自定义token内容，加入组织机构信息
        additionalInfo.put("user_name_type", loginUser.getIdentifierTypeDictId());
        List<RawDataConstraint> rawDataConstraints = loginUser.getRawDataConstraints();
        additionalInfo.put("data_scopes",rawDataConstraints);
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    }
}
